#ifndef __AVI_H
#define __AVI_H
//#include "sys.h"
#include "def.h"

// #define __packed

typedef enum {
	AVI_OK=0,
	AVI_RIFF_ERR,
	AVI_AVI_ERR,
	AVI_LIST_ERR,
	AVI_HDRL_ERR,
	AVI_AVIH_ERR,
	AVI_STRL_ERR,
	AVI_STRH_ERR,
	AVI_STRF_ERR,
	AVI_MOVI_ERR,
	AVI_FORMAT_ERR,
	AVI_STREAM_ERR,
	AVI_STREAM_OVER,
	AVI_IX00,
}AVISTATUS;



#define AVI_RIFF_ID			0X46464952
#define AVI_AVI_ID			0X20495641
#define AVI_LIST_ID			0X5453494C
#define AVI_HDRL_ID			0X6C726468
#define AVI_MOVI_ID			0X69766F6D
#define AVI_STRL_ID			0X6C727473

#define AVI_AVIH_ID			0X68697661
#define AVI_STRH_ID			0X68727473
#define AVI_STRF_ID			0X66727473
#define AVI_STRD_ID			0X64727473

#define AVI_VIDS_STREAM		0X73646976
#define AVI_AUDS_STREAM		0X73647561


#define AVI_VIDS_FLAG		0X6463
#define AVI_AUDS_FLAG		0X7762

#define AVI_FORMAT_MJPG		0X47504A4D


typedef struct
{
	u32 SecPerFrame;
	u32 TotalFrame;
	u32 Width;
	u32 Height;
	u32 SampleRate;
	u16 Channels;
	u16 AudioBufSize;
	u16 AudioType;
	u16 StreamID;
	u32 StreamSize;
	u8* VideoFLAG;
	u8* AudioFLAG;
}AVI_INFO;


typedef struct
{
	u32 RiffID;
	u32 FileSize;
	u32 AviID;
}AVI_HEADER;

typedef struct
{
	u32 FrameID;
	u32 FrameSize;
}FRAME_HEADER;


typedef struct
{
	u32 ListID;
	u32 BlockSize;
	u32 ListType;
}LIST_HEADER;

typedef struct
{
	u32 BlockID;
	u32 BlockSize;
	u32 SecPerFrame;
	u32 MaxByteSec;
	u32 PaddingGranularity;
	u32 Flags;
	u32 TotalFrame;
	u32 InitFrames;
	u32 Streams;
	u32 RefBufSize;
	u32 Width;
	u32 Height;
	u32 Reserved[4];
}AVIH_HEADER;

typedef struct
{
	u32 BlockID;
	u32 BlockSize;
	u32 StreamType;
	u32 Handler;
	u32 Flags;
	u16 Priority;
	u16 Language;
	u32 InitFrames;
	u32 Scale;
	u32 Rate;
	u32 Start;
	u32 Length;
 	u32 RefBufSize;
    u32 Quality;
	u32 SampleSize;
	struct
	{
	   	short Left;
		short Top;
		short Right;
		short Bottom;
	}Frame;
}STRH_HEADER;

typedef struct
{
	u32	 BmpSize;
 	long Width;
	long Height;
	u16  Planes;
	u16  BitCount;
	u32  Compression;
	u32  SizeImage;
	long XpixPerMeter;
	long YpixPerMeter;
	u32  ClrUsed;
	u32  ClrImportant;
}BMP_HEADER;

typedef struct
{
	u8  rgbBlue;
	u8  rgbGreen;
	u8  rgbRed;
	u8  rgbReserved;
}AVIRGBQUAD;

typedef struct
{
	u32 BlockID;
	u32 BlockSize;
	BMP_HEADER bmiHeader;
	AVIRGBQUAD bmColors[1];
}STRF_BMPHEADER;

typedef struct
{
	u32 BlockID;
	u32 BlockSize;
   	u16 FormatTag;
	u16 Channels;
	u32 SampleRate;
	u32 BaudRate;
	u16 BlockAlign;
	u16 Size;
}STRF_WAVHEADER;

#define	 MAKE_WORD(ptr)	(u16)(((u16)*((u8*)(ptr))<<8)|(u16)*(u8*)((ptr)+1))
#define  MAKE_DWORD(ptr)	(u32)(((u16)*(u8*)(ptr)|(((u16)*(u8*)(ptr+1))<<8)|\
						(((u16)*(u8*)(ptr+2))<<16)|(((u16)*(u8*)(ptr+3))<<24)))


AVISTATUS avi_init(u8 *buf,u16 size);
u16 avi_srarch_id(u8* buf,u16 size,u8* id);
AVISTATUS avi_get_streaminfo(u8* buf);


u8 Avih_Parser(u8 *buffer);
u8 Strl_Parser(u8 *buffer);
u8 Strf_Parser(u8 *buffer);



u16 Search_Movi(u8* buffer);
u16 Search_Fram(u8* buffer);
u32 ReadUnit(u8 *buffer,u8 index,u8 Bytes,u8 Format);

#endif

